package GameMain.View.NewPlayerTeach
{
	import GameMain.Game;
	import GameMain.Macro.MacroEvent;
	import GameMain.Model.Entity;
	import GameMain.Model.EntityMove;
	import GameMain.View.PTGame.GameManager;
	
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	import library.SocketData.SocketData;

	public class NewPlayerTeach
	{
		
		private var _currentPre:int = -1;
		private var _mapID:int = 1001;
		private var _step1:MovieClip = null;
		private var _step1JiePai:MovieClip = null;
		private var _step2:MovieClip = null;
		private var _teachStep:int = 0;
		private var _teacher:Entity = null;
		
		public function NewPlayerTeach(info:LoaderInfo)
		{
			var _class:Class = info.applicationDomain.getDefinition("Fla.NewPlayer.STEP1") as Class;
			this._step1 = new _class as MovieClip;
			_class = info.applicationDomain.getDefinition("Fla.NewPlayer.STEP2") as Class;
			this._step2 = new _class as MovieClip;
			this._step1.stop();
		}
		
		public function start():void
		{
			Game.EngineMgr.registerEvent(MacroEvent.MSG_CLIENT_TO_CLIENT,onPlayerAction);
			this._teachStep = 0;
			GameManager.PVP_Game.regesterEvent();
			if(GameManager.BooleanLoad == false)
			{
				Game.ModuleMgr.showModuleFromIMD(Game.loadingUI);
				GameManager.setCallBackFun(onLoadSource);
				GameManager.LoadProfessionModel();
			}
			else
			{
				setTimeout(this.onLoadSourceOver,1000);
			}
		}
		
		private function onLoadSource(per:int):void
		{
			if(this._currentPre == per)
				return;
			this._currentPre = per;
			Game.loadingUI.setLoadingInfo(per);
			if( per == 100 )
			{
				setTimeout(this.onLoadSourceOver,1000);
			}	
		}
		
		private function onPlayerAction(action:int):void
		{
			if(this._teachStep == 0)
				return;
			switch(action)
			{
				case Entity.ACTION_MOVE_FORWARD:
					if(this._teachStep >= 8)
						return;
					this._teachStep++;
					this.setEntityAction(Game.ownPlayer.entityID,action);
					this.moveEntity(Game.ownPlayer.entityID);
					if(this._teachStep == 2)
					{
						this._teacher.removeChild(this._step1);
						this.setStepPos(this._step2);
						this._step2.gotoAndPlay("ST1");
						this._teacher.addChild(this._step2);
					}
					if(this._teachStep > 2 && this._teachStep < 8)
					{
						this.setEntityAction(100,action);
						this.moveEntity(100);
					}
					break;
				case Entity.ACTION_NORMAL_ATTACK:
					if(this._teachStep < 8 )
						return;
					this._teachStep++;
					this.setEntityAction(Game.ownPlayer.entityID,action);
					var hp:int = 100 - (this._teachStep - 8)*25;
					this.setPlayerHP(hp,101);
					if(this._teachStep == 9)
					{
						this._step2.gotoAndPlay("ST3");
					}
					else if(this._teachStep == 12)
					{
						this._step2.gotoAndPlay("ST4");
						setTimeout(backToGameErea,5000);
					}
					break;
			}
		}
		
		
		private function onLoadSourceOver():void
		{
			GameManager.PVP_Game.setNewPlayer(true);
			GameManager.PVP_Game.initGameFromData(this._mapID);
			Game.ModuleMgr.showPVPGame();
			this.initPlayer();
			this.createNPC(101,1600);
			this._teacher = Game.EntityMgr.getEntity(100) as Entity;
			setTimeout(this.startTeach,2000);
		}
		
		private function setPlayerHP(value:int,entityID:int):void
		{
			var data:SocketData = new SocketData();
			data.writeInt16(entityID);
			data.writeInt16(value);
			data.position = 0;
			
			Game.EngineMgr.throwEvent(MacroEvent.MSG_FROM_SERVER_PLAYER_HP,data);
		}
		
		private function initPlayer():void
		{
			var data:SocketData = new SocketData();
			data.writeInt16(Game.ownPlayer.entityID);
			data.writeInt16(100);
			data.writeInt16(0);
			data.position = 0;
			
			Game.EngineMgr.throwEvent(MacroEvent.MSG_FROM_SERVER_POSITION,data);
			
			
			data.clear();
			data.writeInt16(100);
			data.writeInt16(250);
			data.writeInt16(0);
			data.position = 0;
			
			Game.EngineMgr.throwEvent(MacroEvent.MSG_FROM_SERVER_POSITION,data);
			
		}
		
		private function createNPC(entityID:int,p_x:int):void
		{
			var data:SocketData = new SocketData();
			data.writeInt16(entityID);//entityID
			data.writeInt32(1000);//NPCID
			data.writeInt16(p_x); //x
			data.writeInt16(0);  //y;
			data.writeInt8(1); //face
			data.position = 0;
			
			Game.EngineMgr.throwEvent(MacroEvent.MSG_FROM_SERVER_NPC,data);
		}
		
		
		private function removeEntity(entityID:int):void
		{
			var data:SocketData = new SocketData();
			data.writeInt16(entityID);
			data.position = 0;
			
			Game.EngineMgr.throwEvent(MacroEvent.MSG_FROM_SERVER_ENTITY_REMOVE,data);
		}
		
		private function startTeach():void
		{
			this.setStepPos(this._step1);
			this._teacher.addChild(this._step1);
			this._step1.gotoAndStop(1);
			this._step1JiePai = this._step1["FRONT_ORDER"] as MovieClip;
			this._step1JiePai.gotoAndStop(1);
			this._step1.addEventListener(Event.ENTER_FRAME,onSetp1EnterFrame);
			this._step1.play();
		}
		
		
		private function setStepPos(step:MovieClip):void
		{
			step.y = - 220;
			step.x = 10;
		}
		
		private function onSetp1EnterFrame(e:Event):void
		{
			if(this._step1JiePai.currentFrame == 65)
			{
				this.setEntityAction(100,Entity.ACTION_MOVE_FORWARD);
				this.moveEntity(100);
			}
			else if(this._step1JiePai.currentFrame == this._step1JiePai.totalFrames)
			{
				this._step1.removeEventListener(Event.ENTER_FRAME,onSetp1EnterFrame);
				this._step1.gotoAndPlay("GoGo");
				this._teachStep = 1;
			}
		}
		
		private function setEntityAction(entityID:int,action:int):void
		{
			var data:SocketData = new SocketData();
			data.writeInt16(entityID);
			data.writeInt8(action);
			data.position = 0;
			
			Game.EngineMgr.throwEvent(MacroEvent.MSG_FROM_SERVER_ACTION,data);
		}
		
		private function moveEntity(entityID:int):void
		{
			var data:SocketData = new SocketData();
			data.writeInt16(entityID);
			data.writeInt16(10);
			data.writeInt16(0);
			data.writeInt16(0);
			data.writeInt16(0);
			data.position = 0;
			var e:EntityMove =  Game.EntityMgr.getEntity(entityID)
			setTimeout(this.stopEntity,2000,entityID,e.x,410 - e.y);
			Game.EngineMgr.throwEvent(MacroEvent.MSG_FROM_SERVER_MOVE,data);
			
		}
		
		private function stopEntity(entityID:int,startX:int,startY:int):void
		{
			var _time:Number = 2000/100;
			var _x:Number = 10 * _time + (0*(_time*_time))/2;
			var _y:Number = 0 * _time + (0*(_time*_time))/2;
			var data:SocketData = new SocketData();
			data.writeInt16(entityID);
			data.writeInt16(startX + _x);
			data.writeInt16(startY + _y);
			data.position = 0;
			Game.EngineMgr.throwEvent(MacroEvent.MSG_FROM_SERVER_STOP,data);
			if(this._teachStep == 8)
			{
				this._step2.y = - 380;
				this._step2.gotoAndPlay("ST2");
			}
		}
		
		private function backToGameErea():void
		{
			this._teacher.removeChild(this._step2);
			GameManager.PVP_Game.resetGame();
			Game.EntityMgr.resetEntity();
			Game.ModuleMgr.showGameErea();
		}
		
		
	}
}